<template>
  <div class="dia">
    <el-dialog
      :title="formData.id ? '修改' : '新增'"
      :visible.sync="showDialog"
      width="25%"
      @close="close"
    >
      <el-form ref="form" :model="formData" :rules="formRules">
        <el-form-item
          v-if="!$route.query.id"
          label="所属学科"
          label-width="80px"
        >
          <el-select
            @focus="initData"
            v-model="formData.subjectID"
            placeholder="请选择"
          >
            <el-option
              v-for="obj in list"
              :key="obj.id"
              :label="obj.label"
              :value="obj.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="type == 1"
          label="目录名称"
          prop="directoryName"
          label-width="80px"
        >
          <el-input
            v-model="formData.directoryName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
        <el-form-item v-else label="标签名称" prop="tagName" label-width="80px">
          <el-input
            v-model="formData.tagName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button @click="btnOk" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTagsListApi,
  addTagsApi,
  delTagsApi,
  getTagsInfoApi,
  editTagsApi,
  editTagsStateApi,
} from "@/api/Tags";
import { addMuluApi, editMuluApi } from "@/api/Mulu";
import { getXuekeEasyListApi } from "@/api/Xueke";
export default {
  name: "AddDialog",
  props: {
    type: {
      // 1处理目录 0处理标签
      typeof: [Number, String],
      required: true,
    },
  },
  data() {
    return {
      list: [], //学科列表
      showDialog: false, //控制弹出框展示
      formData: {
        subjectID: "", //学科id
        //directoryName: "", //目录名称
        //tagName: "", //目录名称
      },
      formRules: {
        directoryName: [
          { required: true, message: "这是必填项", trigger: "blur" },
        ],
        tagName: [{ required: true, message: "这是必填项", trigger: "blur" }],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.formData.subjectID = this.$route.query.id * 1;
    }
  },
  methods: {
    close() {
      this.formData = {};
      if (this.$route.query.id) {
        this.formData.subjectID = this.$route.query.id * 1;
      }
    },
    async initData() {
      const res = await getXuekeEasyListApi();
      this.list = res;
    },
    async btnOk() {
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return this.$message.error("必填项不能为空");
      }
      if (this.type == 1) {
        //处理目录
        if (this.formData.id) {
          await editMuluApi(this.formData);
        } else {
          await addMuluApi(this.formData);
        }
      } else {
        //处理标签
        if (this.formData.id) {
          await editTagsApi(this.formData);
        } else {
          await addTagsApi(this.formData);
        }
      }
      this.$message.success("操作成功");
      this.$emit("updata");
      this.showDialog = false;
    },
  },
};
</script>

<style lang='scss' scoped>
.dia {
  ::v-deep .el-dialog__header {
    background-color: #409eff;
    .el-dialog__title {
      color: #fff;
    }
  }
  ::v-deep .el-form-item__label {
    font-weight: 700;
  }
  ::v-deep .el-dialog__close {
    color: #fff;
  }
}
</style>